package com.mrpoid.shequ2;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;

import com.mopoba.app.BaHomeActivity;
import com.mrpoid.core.Emulator;
import com.mrpoid.core.MrpRunner;
import com.yichou.common.utils.FileUtils;


/**
 * 
 * @author Yichou 2013-11-16 2:25:46
 *
 */
public class MainActivity extends Activity implements OnClickListener {
	static final String[] APP_NAMES = {
		"gwyaz.mrp",
		"hxsg.mrp",
		"mpqp.mrp",
		"wbrw.mrp"
	};
	
	View ivAuthor0, ivAuthor1;
	TextView mTvNewCount;
	TextView vLastSelApp;
	int selectAppIndex = 0;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ivAuthor0 = findViewById(R.id.iv_author_ui);
		ivAuthor1 = findViewById(R.id.iv_author_prog);
		
		TextView tv = (TextView) findViewById(R.id.tv_app1);
		tv.setOnClickListener(this);
		selectApp(tv);
		
		findViewById(R.id.tv_app2).setOnClickListener(this);
		findViewById(R.id.tv_app3).setOnClickListener(this);
		findViewById(R.id.tv_app4).setOnClickListener(this);
		
		findViewById(R.id.btn_entry).setOnClickListener(this);
		findViewById(R.id.btn_start).setOnClickListener(this);
		findViewById(R.id.btn_news).setOnClickListener(this);
		
		getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
			@Override
			public void onGlobalLayout() {
				System.err.println("layout finish!");
				
				getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void selectApp(TextView v) {
		if(vLastSelApp != null && vLastSelApp != v) {
			vLastSelApp.setSelected(false);
		}
		v.setSelected(true);
		vLastSelApp = v;
	}
	
	private void startApp(int index) {
		FileUtils.assetToFileIfNotExist(getAssets(), 
				"apps/" + APP_NAMES[index],
				Emulator.getInstance().getVmFullFilePath(APP_NAMES[index]));

		// 复制 system 内容
		File dir = Emulator.getInstance().getVmFullFilePath("system");
		FileUtils.createDir(dir);

		File dst = new File(dir, "userinfo2.sys");
		if (!dst.exists()) {
			File src = new File(Emulator.getInstance().getVmLastFullPath() + "system", "userinfo2.sys");
			if (!src.isFile()) {
				src = new File(Emulator.getInstance().getVmDefaultFullPath() + "system", "userinfo2.sys");
			}
			if (src.isFile())
				FileUtils.copyTo(dst, src);
		}
		
		MrpRunner.runMrp(this, APP_NAMES[index], index, false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_app1:
			selectAppIndex = 0;
			selectApp((TextView) v);
			break;
		case R.id.tv_app2:
			selectAppIndex = 1;
			selectApp((TextView) v);
			break;
		case R.id.tv_app3:
			selectAppIndex = 2;
			selectApp((TextView) v);
			break;
		case R.id.tv_app4:
			selectAppIndex = 3;
			selectApp((TextView) v);
			break;
			
		case R.id.btn_start:
			startApp(selectAppIndex);
			break;
		}
		
		if(v.getId() == R.id.btn_entry) {
			startActivity(new Intent(this, BaHomeActivity.class));
		}
	}
}
